<template>
  <div class="search-history">
    <van-cell title="搜索历史">
      <template v-if="isEdit">
        <span @click="searchHistories.splice(0)">全部删除</span>&nbsp;&nbsp;
        <span @click="isEdit = false">完成</span>
      </template>
      <van-icon @click="isEdit = true" v-else name="delete" />
    </van-cell>
    <van-cell
      v-for="(item, index) in searchHistories"
      :key="index"
      :title="item"
      @click="clickHandler(item, index)"
    >
      <van-icon v-if="isEdit" name="close" />
    </van-cell>
  </div>
</template>
<script>
export default {
  name: "SearchHistory",
  props: {
    searchHistories: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      isEdit: false, // 是否编辑
    };
  },
  methods: {
    clickHandler(val, idx) {
      // 判断行为
      if (this.isEdit) {
        // true则是删除操作
        this.searchHistories.splice(idx, 1);
      } else {
        // false则是点击搜索该历史记录的结果
        this.$emit("search", val);
      }
    },
  },
};
</script>

<style scoped lang="less"></style>
